﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using System.Windows.Forms;
namespace UIT_CV_Management
{
    public class TotNghiep
    {
        public String maTotNghiep;
        private String tenDoAnTotNghiep;
        private float ketQuaDoAn;
        public String maMonThiToiNghiep;

        // object
        public List<MonThiTotNghiep> listMonThiTotNghiep;
        
        
        //Database
        Database myDatabase = Database.shareDatabase;
        public List<MonThiTotNghiep> getListMonThiTotNghiep()
        {
            QueryResult result_MonThi = myDatabase.Query("SELECT * FROM MonThiTotNghiep WHERE MaMonThiTotNghiep = '" + maMonThiToiNghiep + "'");
            if (result_MonThi.ReturnCode == 200)
            {
                listMonThiTotNghiep = new List<MonThiTotNghiep>();

                for (int i = 0; i < result_MonThi.Table.Rows.Count; i++)
                {
                    DataRow row = result_MonThi.Table.Rows[i];
                    MonThiTotNghiep monThi = new MonThiTotNghiep(row["ID"].ToString());
                    listMonThiTotNghiep.Add(monThi);
                }
            }
            return listMonThiTotNghiep;
        }
        public TotNghiep(String _tenDoAn, float _ketQuaDoAn, List<MonThiTotNghiep> _listMonThiTotNghiep)
        {
            maTotNghiep = Database.getID("TotNghiep", "MaTotNghiep");

            tenDoAnTotNghiep = _tenDoAn;
            ketQuaDoAn = _ketQuaDoAn;
            
            //object
            listMonThiTotNghiep = _listMonThiTotNghiep;
            maMonThiToiNghiep =  _listMonThiTotNghiep[0].maMonThiTotNghiep;
        }
        public TotNghiep(String _MaTotNghiep)
        {
            QueryResult result =  myDatabase.Query("SELECT * FROM TotNghiep WHERE MaTotNghiep = '" + _MaTotNghiep + "'");
            if (result.ReturnCode == 200)
            {
                DataTable table = result.Table;
                maTotNghiep = table.Rows[0]["MaTotNghiep"].ToString();
                tenDoAnTotNghiep = table.Rows[0]["TenDoAnTotNghiep"].ToString();
                ketQuaDoAn = float.Parse(table.Rows[0]["KetQuaDoAn"].ToString());
                maMonThiToiNghiep = table.Rows[0]["MaMonThiTotNghiep"].ToString();
            }
        }
        public Boolean Save()
        {
            

            String sqlTotNghiep = "INSERT INTO TotNghiep VALUES('"
                + maTotNghiep + "',N'" 
                + tenDoAnTotNghiep + "'," 
                + ketQuaDoAn + ",'" 
                + maMonThiToiNghiep + "')";
            QueryResult result = myDatabase.Query(sqlTotNghiep);
            if (result.ReturnCode == 409)
                return false;
            else // save successfully
            {
                foreach (MonThiTotNghiep monThi in listMonThiTotNghiep)
                {
                    monThi.Save();
                }
                return true;
            }
        }
        public Boolean Update()
        {
            bool isDupplicated = Database.isDupplicate("TotNghiep", "MaTotNghiep", maTotNghiep);

            // update
            if (isDupplicated)
            {
                String sqlUpdate = "UPDATE TotNgiep SET TenDoAnTotNghiep = N'" + tenDoAnTotNghiep + "', KetQuaDoAn = " + ketQuaDoAn
                    + " WHERE MaTotNgiep = '" + maTotNghiep + "'";
                QueryResult result = myDatabase.Query(sqlUpdate);

                if (result.ReturnCode == 409)
                    return false;
            }
            return true;
        }
        public Boolean Delete()
        {
            String sqlDelete = "DELETE FROM TotNghiep WHERE MaTotNghiep = '" + maTotNghiep + "'";
            QueryResult result = myDatabase.Query(sqlDelete);

            if (result.ReturnCode == 409)
                return false;
            else
            {
                foreach (MonThiTotNghiep monThi in listMonThiTotNghiep)
                {
                    monThi.Delete();
                }
                return true;
            }
        }
    }
}
